﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PominaScale.Properties;

namespace PominaScale
{
    public partial class FrmWarehouse : Form
    {
        public FrmWarehouse()
        {
            InitializeComponent();

            DirectoryInfo dir = new DirectoryInfo(Settings.Default.MM_S2C);
            foreach (var item in dir.GetFiles("*.csv"))
            {
                chkListInbound1.Items.Add(item.Name);
            }

            dir = new DirectoryInfo(Settings.Default.MM_C2S);
            foreach (var item in dir.GetFiles("*.csv"))
            {
                chkListInbound2.Items.Add(item.Name);
            }

            dir = new DirectoryInfo(Settings.Default.SD_S2C);
            foreach (var item in dir.GetFiles("*.csv"))
            {
                chkListOutbound1.Items.Add(item.Name);
            }

            dir = new DirectoryInfo(Settings.Default.SD_C2S);
            foreach (var item in dir.GetFiles("*.csv"))
            {
                chkListOutbound2.Items.Add(item.Name);
            }

            this.btnSave.Click += new EventHandler(delegate {
                int i;
                string source, target;
                i = 0;
                while (i < chkListInbound1.Items.Count)
                {
                    if (chkListInbound1.GetItemChecked(i))
                    {
                        source = Settings.Default.MM_S2C + chkListInbound1.Items[i].ToString();
                        target = Settings.Default.Stored + "\\MM_S2C\\" + chkListInbound1.Items[i].ToString();
                        File.Move(source, target);
                        chkListInbound1.Items.RemoveAt(i);
                    }
                    else
                        i++;
                }

                i = 0;
                while (i < chkListInbound2.Items.Count)
                {
                    if (chkListInbound2.GetItemChecked(i))
                    {
                        source = Settings.Default.MM_C2S + chkListInbound2.Items[i].ToString();
                        target = Settings.Default.Stored + "\\MM_C2S\\" + chkListInbound2.Items[i].ToString();
                        File.Move(source, target);
                        chkListInbound2.Items.RemoveAt(i);
                    }
                    else
                        i++;
                }

                i = 0;
                while (i < chkListOutbound1.Items.Count)
                {
                    if (chkListOutbound1.GetItemChecked(i))
                    {
                        source = Settings.Default.SD_S2C + chkListOutbound1.Items[i].ToString();
                        target = Settings.Default.Stored + "\\SD_S2C\\" + chkListOutbound1.Items[i].ToString();
                        File.Move(source, target);
                        chkListOutbound1.Items.RemoveAt(i);
                    }
                    else
                        i++;
                }

                i = 0;
                while (i < chkListOutbound2.Items.Count)
                {
                    if (chkListOutbound2.GetItemChecked(i))
                    {
                        source = Settings.Default.SD_C2S + chkListOutbound2.Items[i].ToString();
                        target = Settings.Default.Stored + "\\SD_C2S\\" + chkListOutbound2.Items[i].ToString();
                        File.Move(source, target);
                        chkListOutbound2.Items.RemoveAt(i);
                    }
                    else
                        i++;
                }

            });

            this.btnClose.Click += new EventHandler(delegate  { this.Close(); });

            this.chkInbound.CheckedChanged += new EventHandler(delegate {
                bool b = (chkInbound.Checked);

                for (int i = 0; i < chkListInbound1.Items.Count; i++)
                {
                    chkListInbound1.SetItemChecked(i, b);
                }

                for (int i = 0; i < chkListInbound2.Items.Count; i++)
                {
                    chkListInbound2.SetItemChecked(i, b);
                }
                
            });

            this.chkOutbound.CheckedChanged += new EventHandler(delegate
            {
                bool b = (chkOutbound.Checked);

                for (int i = 0; i < chkListOutbound1.Items.Count; i++)
                {
                    chkListOutbound1.SetItemChecked(i, b);
                }

                for (int i = 0; i < chkListOutbound2.Items.Count; i++)
                {
                    chkListOutbound2.SetItemChecked(i, b);
                }

            });
        }

       
    }
}
